import user.User;
import user.factory.AdminUserFactory;
import user.factory.IUserFactory;
import user.factory.NormalUserFactory;
import user.factory.ProxyUser;

import javax.sql.rowset.spi.SyncResolver;
import java.util.Scanner;

public class LibrarySystem {
    public static void main(String[] args) {
        // 工厂模式
        IUserFactory adminUserFactory = new AdminUserFactory();
        User admin = adminUserFactory.creatUser("刘备",1);

        IUserFactory normalUserFactory1 = new NormalUserFactory();
        User normal1 = normalUserFactory1.creatUser("关羽",2);

        IUserFactory normalUserFactory2 = new NormalUserFactory();
        User normal2 = normalUserFactory2.creatUser("张飞",3);

        // 代理模式
        ProxyUser proxyUserAdmin = new ProxyUser(admin);
        ProxyUser proxyUserNormal1 = new ProxyUser(normal1);
        ProxyUser proxyUserNormal2 = new ProxyUser(normal2);

        LibrarySystem librarySystem = new LibrarySystem();
        ProxyUser currentUser= librarySystem.selectProxyRole(proxyUserAdmin,proxyUserNormal1,proxyUserNormal2);

        while(true){
            //选择什么角色 就进行什么操作
            int choice = currentUser.display();

            currentUser.handleOperation(choice);
        }


    }

    public ProxyUser selectProxyRole(ProxyUser proxyUserAdmin, ProxyUser proxyUserNormal1, ProxyUser proxyUserNormal2) {
        System.out.println("选择角色登录：");
        System.out.println("1.管理员\n2.普通用户(关羽)\n3.普通用户(张飞)\n4.退出系统");
        ProxyUser currentUser = null;

        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();

        switch(choice){
            case 1:
                currentUser = proxyUserAdmin;
                break;
            case 2:
                currentUser = proxyUserNormal1;
                break;
            case 3:
                currentUser = proxyUserNormal2;
                break;
            case 4:
                System.exit(0);
                System.out.println("退出系统...");
                break;
            default:
                break;

        }
        return currentUser;
    }
}
